<template>
  <div class="common-table">
    <el-table :data="tableData" height="100%" stripe v-loading="config.loading">
      <el-table-column label="序号" width="85">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-for="label in tableLabel"
        :key="label.prop" 
        :label="label.label"
        :prop="label.prop"
        show-overflow-tooltip
        :min-width="label.width ? label.width : 120"
        align="center"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{scope.row[label.prop]}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination 
      class="manage-pagination" 
      layout="prev, pager, next" 
      :page-count="config.total" 
      :current-page.sync="config.page" 
      @current-change="changePage"
      v-if="config.total && tableData.length != 1"
    />
  </div>
</template>
<script>
export default {
  name: 'CommonTable',
  methods: {
    handleEdit(row) {
      this.$emit('handleEdit', row)
    },
    handleDelete(row) {
      this.$emit('handleDelete', row)
    },    
    // 切换页码
    changePage(page) {
      this.$emit('changePage', page)
    }
  },
  props: {
    tableData: {
      type: Array,
      default() {
        return []
      }
    },
    tableLabel: {
      type: Array,
      default() {
        return []
      }
    },
    config: {
      type: Object,
      default() {
        return {}
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  .common-table {
    height: calc(95% - 60px);
  }
  .manage-pagination {
    position: absolute;
    bottom: 15px;
    right: 0;
  }
</style>